package com.woniu.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.common.CommonResult;
import com.woniu.pojo.*;
import com.woniu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/propertypay")
public class PayController {

    @Autowired
    private PayService payService;

    @Autowired
    private ServiceImgService serviceImgService;

    @Autowired
    private ServiceOrderService serviceOrderService;

    @Autowired
    private ServiceCollectService serviceCollectService;

    @Autowired
    private ServiceEvaluateService serviceEvaluateService;


    //通过物业id获取物业服务列表
    @GetMapping("/list")
    public @ResponseBody
    CommonResult propertylist() {
        Integer userid=1;
        Integer propertyid=payService.findpropertyidByUserId(userid);
        try {
            List<Propertypaydto> propertypays = payService.findserviceByPropertyId(propertyid);
            return CommonResult.success(propertypays);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取数据失败");
        }
    }

    //根据服务id获取服务信息
    @GetMapping("/getdetail/{serviceorderid}")
    public @ResponseBody CommonResult getdetail(@PathVariable("serviceorderid")Integer serviceorderid)
    {
        try {
            ServiceOrderdto serviceOrderdto=serviceOrderService.selectByid(serviceorderid);
            return CommonResult.success(serviceOrderdto);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("获取数据失败");
        }

    }

    //收藏服务
    @RequestMapping("/collectservice")
    @ResponseBody
    public CommonResult collectService(CollectService collectService){
        collectService.setUserid(1);
        Integer serviceid=serviceCollectService.selectserviceidByuserid(collectService.getUserid());
        if(serviceid==collectService.getServiceid()){
            return CommonResult.failed("重复收藏,收藏失败");
        }
        try {
            serviceCollectService.addCollectService(collectService);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("收藏失败");
        }
        return CommonResult.success("收藏成功");
    }

    //删除收藏服务
   @RequestMapping("/deleteCollectservice")
    @ResponseBody
    public CommonResult deletecollectService(Integer serviceid)
    {
        System.out.println(serviceid);
       Integer userid=1;
        try {
            serviceCollectService.deleteCollectService(serviceid,userid);
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  CommonResult.failed("删除失败");
        }
    }

    //添加服务评论
    @PostMapping("/addevaluate")
    @ResponseBody
    public CommonResult addServiceEvaluate(EvaluateService evaluateService)
    {
        try {
            serviceEvaluateService.addevaluate(evaluateService);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("添加失败");
        }
    }

    //获取服务详细信息及评论
    @GetMapping("/detail/{serviceid}")
    public @ResponseBody CommonResult servicedetail(@PathVariable("serviceid") Integer serviceid) {
        try {
            List<ServiceImg> serviceImgs=serviceImgService.findServiceImgByServiceId(serviceid);
            Pay pay=payService.finddetailByPayId(serviceid);
            List<Evaluatedto> evaluatedtos=serviceEvaluateService.selectevaluateByServiceid(serviceid);
            HashMap map=new HashMap<>();
            map.put("imgs",serviceImgs);
            map.put("detail",pay);
            map.put("evaluate",evaluatedtos);
            return CommonResult.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("查询商品失败");
        }
    }



}